$(function() {

	$('#viewTable').bootstrapTable({
		data : getDatas(),
		striped : true,
		cache : false,
		pagination : true,
		sortable : false,
		toolbar : "#verifyToolBar",
		sidePagination : "client",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 25, 50, 100 ],
		search : true,
		strictSearch : false,
		showColumns : true,
		clickToSelect : true,
		uniqueId : 'id',
		columns : [ 
		{
			field : 'customerName',
			title : '报修客户'
		}, {
			field : 'deviceName',
			title : '报修设备'
		}, {
			field : 'createTime',
			title : '报修时间',
			formatter : function(value, row, index) {
				return moment(value).format('YYYY-MM-DD HH:mm:ss');
			}
		}, {
			field : 'finishTimeShow',
			title : '完成时间'
		},{
			field : 'billStatusShow',
			title : '当前状态'
		} ],
		onDblClickRow : function(row) {// 双击查看详情
			showBillDetail(row);
		}
	});

	$("#viewTable").bootstrapTable("hideColumn", "id");
});

 
//0:审核拒绝，1待审核，2审核通过，待提交维修地址，3维修地址已提交，待维修师抢单，4维修师已抢单，待维修，5维修师已完成，待评价 6维修已完成
function getDatas() {
	var bills = '';
	$.ajax({
		url : "../bill/queryMultiStatus.action",
		type : "post",
		async : false,// 是为了内层ajax在之后访问
		data: {"billStatus":"5,6"},//待审核
		success : function(data) {
			$(data).each(function() {
				console.log(this);
				this.billDescHideOverflow = '<div class=\'td-cont-3\'>'+this.billDesc + '</div>';
				this.finishTimeShow = getBillFinishTime(this.id, this.billStatus, this.updateTime);
				this.billStatusShow = getBillStatus(this.billStatus);
			});
			bills = data;
		},
		error : function() {
			toastr.error("获取报修信息发生出错，请联系管理员");
		}
	});
	return bills.reverse();
}


function getBillStatus(status) {
	if (status == '0') {
		return "审核拒绝"
	} else if (status == '1') {
		return "待审核";
	} else if (status == '2') {
		return "审核通过，待提交维修地址";
	} else if (status == '3') {
		return "维修地址已提交，待维修师抢单";
	} else if (status == '4') {
		return "维修师已抢单，待维修";
	} else if (status == '5') {
		return "维修师已完成，待评价";
	} else if (status == '6') {
		return "客户评价完成";
	}
}

function getBillFinishTime(billId, billStatus, updateTime) {
	var finishTime = '';
	if (billStatus == '5') {
		//用户未评价
		$.ajax({
			url: "../repair/query.action",
			type: "post",
			data: {"billId": billId},
			async: false,
			success: function(data) {
				finishTime = data[0].updateTime;
			},
			error: function() {
				toastr.error("获取维修完成时间出错，请联系管理员");
			}
		});
	} else {
		finishTime = updateTime;
	}
	console.log(billId+'----'+billStatus+'----'+finishTime);
	return moment(finishTime).format('YYYY-MM-DD HH:mm:ss')
}

//详情
function showBillDetail(bill) {
	var contract = getContractById(bill.contractId);
	
	var billDetailHtml = '<p style="text-align: center; color: #6294AD;"><b>基本信息</b></p>'
		+	'<p>id：' + bill.id + '</p>'
		+	'<p>客户姓名：' + bill.customerName + '</p>'
		+	'<p>报修设备：' + bill.deviceName + '</p>'
		+	'<p>报修时间：' + moment(bill.createTime).format('YYYY-MM-DD HH:mm:ss') + '</p>'
		+	'<p>损坏描述：<pre style="white-space: pre-wrap !important;word-wrap: break-word !important;">' + bill.billDesc + '</pre></p>'
		+   '<p>完成时间：' + bill.finishTimeShow + '</p>'
	if (bill.billStatus == '6') {
		billDetailHtml += '<p>客户评价：'+bill.billAssess + '</p>';
	}
	
	billDetailHtml += '<hr/><p style="text-align: center; color: #6294AD;"><b>日志记录</b></p>'
	$.ajax({
		url: "../billLog/query.action",
		type: "post",
		data: {"billId": bill.id},
		async: false,
		success: function(data) {
			$(data).each(function(){
				var operateUserName = '';
				if (this.operateUserType == '2') {
					operateUserName = getUserNameById(this.operateUserId);
				} else if (this.operateUserType == '1') {
					var cus = getCustomerById(this.operateUserId);
					operateUserName = cus.customerName;
				}
				billDetailHtml += '<p>操作人：'+operateUserName+'&nbsp;&nbsp;&nbsp;&nbsp; 操作事件：'+this.operateEvent+'&nbsp;&nbsp;&nbsp;&nbsp; 操作时间：'+moment(this.operateTime).format('YYYY-MM-DD HH:mm:ss')+'<p>';
			});
		},
		error: function() {
			toastr.error("获取报修日志记录发生错误，请稍后尝试");
		}
	});
	
	

	$("#billDetailShow").html(billDetailHtml);
	$("#billDetailModal").modal("show");
}

function verify(id, verifyResult) {
	formReset("#verifyForm");
	$("#verifyForm [name='billId']").val(id);
	$("#verifyForm [name='verifyResult']").val(verifyResult);
	$("#verifyModal").modal("show");
}

function verifySave() {
	if (!$("#verifyForm").valid()) {
		return;
	}
	var data = $("#verifyForm").serialize();
	//console.log(data);
	$.ajax({
		url: "../verify/save.action",
		type: "post",
		data: data,
		success: function (data) {
			if (data.success == "true") {
				toastr.success("审核成功");
				$("#viewTable").bootstrapTable("load", getDatas());
			} else {
				toastr.error("后台处理发生错误，请联系管理员");
			}
		},
		error: function() {
			toastr.error("提交请求发生错误，请联系管理员");
		}
	});
	$("#verifyModal").modal("hide");
}